<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/2 0002
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">身份证号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="identity" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入身份证号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">登录名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="loginName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">真实名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="realName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入真实名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">地址:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入地址" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">电话号码:</label>
    <div class="layui-input-inline" style="padding: 5px">
        <input type="text" name="phone" autocomplete="off" class="layui-input layui-input-inline"
               placeholder="请输入电话号码" style="height: 30px;border-radius: 10px">
    </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">职位:</label>
        <div class="layui-input-inline" style="padding: 5px">
            <input type="text" name="position" autocomplete="off" class="layui-input layui-input-inline"
                   placeholder="请输入职位" style="height: 30px;border-radius: 10px">
        </div>
    </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-black" style="text-align: center">
            <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm " lay-submit lay-filter="formDemo">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->

<table id="userTable" lay-filter="userTable"></table>

<script id="toolbarUser" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="userAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="userDelChk">批量删除</button>
    </div>
</script>

<script id="barUser" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="resetPwd">重置密码</a>
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="selUserMenu">分配角色</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
</script>

<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="addOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">身份证ID:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="hidden" name="userId">
                <input type="text" name="identity" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入身份证号码" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">登录名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="loginName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登录密码:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="password" name="pwd" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录密码" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">真实姓名:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="realName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入真实姓名" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" checked title="男">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
            <label class="layui-form-label">地区:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入地区" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="phone" name="phone" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入电话" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">职位:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="position" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入职位" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户类型:</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="1"  title="超级管理员">
                    <input type="radio" name="type" value="0" checked title="系统用户">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用:</label>
                <div class="layui-input-block">
                    <input type="radio" name="available" value="1"  checked title="可用">
                    <input type="radio" name="available" value="0"  title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>

<%--分配角色开始--%>
<div style="display: none;padding: 20px" id="selUserRoleDiv">
    <table id="roleTable" lay-filter="roleTable"></table>
</div>
<%--分配角色结束--%>
<script src="/layui/layui.js"></script>
<script>
    var userTable
    var index
    var url
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['table','form','layer','jquery','dtree'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery
        var dtree=layui.dtree

        // 初始table
        userTable = table.render({
            elem: "#userTable"
            , url: "/user/findUser"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbarUser'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'userId', title: '用户ID', fixed: 'center'}
                , {field: 'loginName', title: '登录名称', fixed: 'center'}
                , {field: 'identity', title: '身份证号', fixed: 'center'}
                , {field: 'realName', title: '真实姓名', fixed: 'center'}
                , {field: 'sex', title: '性别', fixed: 'center' ,templet: function (data) {
            return data.sex == 1 ? "男" : "女"
        }}
                , {field: 'address', title: '家庭地址', fixed: 'center'}
                , {field: 'position', title: '公司职称', fixed: 'center'}
                , {field: 'phone', title: '联系方式', fixed: 'center'}
                , {field: 'type', title: '类型', fixed: 'center',templet: function (data) {
                        return data.sex == 1 ? "超级管理员" : "系统用户"}}
                , {
                    field: 'available', title: '是否可用', fixed: 'center', templet: function (data) {
                        return data.available == 1 ? "可用" : "<font color=red >不可用</font>"
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barUser', width: 250}
            ]]
        })


        // 查询
        form.on("submit(formDemo)", function (data) {
            userTable.reload({
                url: "/user/findUser"
                , where: {
                    'identity': data.field.identity
                    , 'loginName':data.field.loginName
                    ,'realName':data.field.realName
                    ,'address':data.field.address
                    ,'position':data.field.position
                    ,'phone':data.field.phone

                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(userTable)',function(obj) {
            if(obj.event == "userAdd"){ // 点击新增按钮执行的内容
                userAdd()
            }else if(obj.event=="userDelChk"){
                userDelChk(obj)
            }
        })
        //监听行工具事件
        table.on('tool(userTable)', function(obj){
            var data = obj.data
            if(obj.event == "edit"){
                menuUpdate(data)
            }else  if(obj.event == "del"){
                del(data)
            }else if(obj.event=="selUserMenu"){
                selUserMenu(data)
            }else if(obj.event=="resetPwd"){
                resetPwd(data)
            }
        })
        //重置密码
        function resetPwd(data) {
            layer.confirm("是否重置密码为123456",function (ind) {
                $.post("/user/resetPwd",{"userId":data.userId},function (r) {
                    layer.msg(r.msg)
                    userTable.reload()
                })
                layer.close(ind)
            })
        }
        //分配角色
        var roleTable
        function  selUserMenu(data) {
            // 弹出框
            index =  layer.open({
                type:1 // 1（页面层）
                , title:"分配"+data.realName+"的角色"
                , content:$("#selUserRoleDiv")
                , area : ['600px', '380px']
                , btn:['<div class="layui-icon layui-icon-release">确认分配</div>'
                    ,'<div class="layui-icon layui-icon-close">取消分配</div>']
                ,btnAlign:'c'
                , success:function() {
                    // 给roleTable赋值
                    roleTable =  table.render({
                        elem:"#roleTable"
                        ,url:"/user/findRoleByUser?userId="+data.userId
                        // ,page: {limit:5 ,limits:[5,10,15]} //开启分页
                        ,cellMinWidth:80
                        ,cols: [[ //表头
                            {type: 'checkbox', fixed: 'left'}
                            ,{field: 'roleId', title: '角色ID',  fixed: 'center'}
                            ,{field: 'roleName', title: '角色名称', fixed: 'center'}
                            ,{field: 'roleDesc', title: '角色描述',  fixed: 'center'}
                        ]]
                    })
                }
                , yes : function(obj){
                    var checkStatus = table.checkStatus("roleTable");
                    if(checkStatus.data.length == 0){
                        layer.msg("至少选择一个")
                    }else{
                        // 获取用户id
                        var userId = data.userId
                        var param = "userId="+userId

                        // 循环选中的内容nodes
                        $.each(checkStatus.data,function(i,item){
                            param += "&ids=" +item.roleId
                        })
                        // A=valueA&B=valueB&B=valueB
                        // 把数据保存到数据库
                        $.get("/user/saveRoleUser?"+param,function (r) {
                            layer.msg(r.msg)
                        })
                    }


                    layer.close(index)
                }
            })
        }

        function userDelChk(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var data=checkStatus.data
            if(data.length==0){
                layer.msg("请至少选择一个数据")
            }else {
                var userIds=new Array()
                $.each(data,function (index,item) {
                    userIds.push(item.userId)
                })
                layer.confirm("你确定要删除"+data.length+"条数据么",function (ind) {
                    $.post("/user/userDelChk",{"userIds":userIds},function (r) {
                        layer.msg(r.msg)
                        userTable.reload()
                    })
                    layer.close(ind)
                })
            }
        }
        function del(data){

            $.post("/user/chkRoleUser",{"userId":data.userId},function (r) {
                if(r.code>0){
                    alert("该角色"+data.realName+"有关联数据。")
                }
            })
            layer.confirm("您确定要删除"+ data.realName +"数据么",function(ind){

                $.post("/user/delUser", {"userId":data.userId} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    userTable.reload()

                })
                layer.close(ind)
            })


        }
        function menuUpdate(data) {
            index =  layer.open({
                type:1
                , title:"修改用户"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    form.val("dataFrm",data)
                    url="/user/updateUser"
                }
            })
        }
        function userAdd() {
            // 弹出框
            index =  layer.open({
                type:1 //
                , title:"添加用户"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    url="/user/addUser"
                }
            })

        }
        // form表达提交
        form.on("submit(doSubmit)",function() {
            layer.confirm("您确定要提交该数据么",function(ind){
                $.post(url,$("#dataFrm").serialize(),function(data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    userTable.reload()
                })
                layer.close(ind)
            })





        })
    })

</script>
</body>
</html>